package com.example.lianjuan.Presenter.impl.impl;

import com.example.lianjuan.Model.API;
import com.example.lianjuan.Model.domain.TicketParams;
import com.example.lianjuan.Model.domain.TicketResult;
import com.example.lianjuan.Presenter.impl.ITicketPresenter;
import com.example.lianjuan.Utils.LogUtils;
import com.example.lianjuan.Utils.RetrofitManang;
import com.example.lianjuan.Utils.UtilsUrl;
import com.example.lianjuan.View.ITickPagerCallback;

import java.net.HttpURLConnection;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class TicketPresenterImpl implements ITicketPresenter {
    private ITickPagerCallback callback = null;
    private String mCover;
    private TicketResult mResult;

    enum LoadState {
        NONE, ERROR, SUCCESS, LOADING
    }

    private LoadState mCURRENT = LoadState.NONE;

    @Override
    public void getTicket(String title, String url, String cover) {
        mCover = cover;
        mCURRENT = LoadState.LOADING;
        LogUtils.d(this, "title---->" + title);
        onTicketLoading();
        LogUtils.d(TicketPresenterImpl.this, "url------>" + url);
        LogUtils.d(TicketPresenterImpl.this, "cover------>" + mCover);
        String ticketUrl = UtilsUrl.getTicketUrl(url);
        LogUtils.d(this, "ticketUrl------>" + ticketUrl);
        Retrofit retrofit = RetrofitManang.getRetrofitManang().getRetrofit();
        API api = retrofit.create(API.class);
        TicketParams ticketParams = new TicketParams(ticketUrl, title);
        Call<TicketResult> tickerResult = api.getTickerResult(ticketParams);
        tickerResult.enqueue(new Callback<TicketResult>() {
            @Override
            public void onResponse(Call<TicketResult> call, Response<TicketResult> response) {
                int code = response.code();
                LogUtils.d(TicketPresenterImpl.this, "请求代码----->" + code);
                if (code == HttpURLConnection.HTTP_OK) {
                    //请求成功
                    mResult = response.body();
                    mCURRENT = LoadState.SUCCESS;
                    onTicketLoadedSuccess();
                } else {
                    //请求失败
                    mCURRENT = LoadState.ERROR;
                    onTicketLoadedError();
                }
            }

            @Override
            public void onFailure(Call<TicketResult> call, Throwable t) {
                mCURRENT = LoadState.ERROR;
                onTicketLoadedError();
            }
        });
    }

    private void onTicketLoadedSuccess() {
        if (callback != null) {
            callback.onTicketLoaded(mCover, mResult);
        }
    }

    /**
     * 请求失败的逻辑
     */
    private void onTicketLoadedError() {
        if (callback != null) {
            callback.onLoadError();
        }
    }


    @Override
    public void registerViewCallback(ITickPagerCallback callback) {
        this.callback = callback;
        //网络请求速度过快，导致UI还没持有对象
        if (mCURRENT != LoadState.NONE) {
            //说明状态发生改变
            //通知UI改变
            if (mCURRENT == LoadState.SUCCESS) {
                onTicketLoadedSuccess();
            } else {
                if (mCURRENT == LoadState.ERROR) {
                    onTicketLoadedError();
                } else {
                    if (mCURRENT == LoadState.LOADING) {
                        onTicketLoading();
                    }
                }
            }
        }
    }

    private void onTicketLoading() {
        if (callback != null) {
            callback.onLoadLoading();
        }
    }

    @Override
    public void unregisterViewCallback(ITickPagerCallback callback) {
        this.callback = null;
    }

}
